package com.google.gwt.livinglife.server.data;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.Inheritance;
import javax.jdo.annotations.InheritanceStrategy;
import javax.jdo.annotations.Order;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;

import com.google.gwt.livinglife.client.data.BasicPostWithStatsData;
import com.google.gwt.livinglife.client.data.ChallengeData;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
@Inheritance(customStrategy = "complete-table")
public class Challenge extends BasicPostWithStats {

	// We keep number of responses separately to be able to sort based on this field.
	@Persistent
	int num_responses = 0;
	
	@Persistent
	Long group_id;
	
	@Persistent
	String group_name;
	
	public String getGroup_name() {
		return group_name;
	}

	public void setGroup_name(String groupName) {
		group_name = groupName;
	}

	@Persistent
	@Order(extensions = @Extension(vendorName="datanucleus", key="list-ordering", value="date asc"))
	List<Long> response_ids = new ArrayList<Long>();
	
	public Challenge() {
		super();
	}
	
	public Challenge(ChallengeData data) {
		super((BasicPostWithStatsData)data);
		group_id = data.getGroup_id();
		group_name = data.getGroup_name();
	}

	public List<Long> getResponses() {
		return response_ids;
	}
	
	public Long getGroup_id() {
		return group_id;
	}

	public void setGroup_id(Long groupId) {
		group_id = groupId;
	}

	public void setResponses(List<Long> responses) {
		this.response_ids = responses;
		num_responses = responses.size();
	}
	
	public void addResponse(Long response_id) {
		response_ids.add(response_id);
		++num_responses;
	}
}
